package com.sline.day01.jdbc;

import org.junit.Test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

public class Demo3_DML {

    public static void main(String[] args) throws Exception {
        DMLTest aaa = new DMLTest();
        aaa.insert();
    }
}

// 针对t_student表进行操作
class DMLTest {
    // 插入信息陆承东，27
    @Test
    public void insert() throws Exception {
        String sql = "insert into t_student(name, age) values ('乔峰', 27);";

        Class.forName("com.mysql.jdbc.Driver");

        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbcdemo", "root", "admin");

        Statement statement = connection.createStatement();

        int row = statement.executeUpdate(sql);
        System.out.println("成功操作" + row + "");

        statement.close();
        connection.close();
    }

    // 更新操作，将乔峰，33修改为萧峰，34
    @Test
    public void update() throws Exception {
        String sql = "update t_student set name = '萧峰', age = 34 where id = 2;";

        Class.forName("com.mysql.jdbc.Driver");

        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbcdemo", "root", "admin");

        Statement statement = connection.createStatement();

        int row = statement.executeUpdate(sql);
        System.out.println("成功操作" + row);

        statement.close();
        connection.close();
    }

    // 删除操作，将id为1的人删除
    @Test
    public void delete() throws Exception {
        String sql = "delete from t_student where id = 1;";

        Class.forName("com.mysql.jdbc.Driver");

        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbcdemo", "root", "admin");

        Statement statement = connection.createStatement();

        int row = statement.executeUpdate(sql);
        System.out.println("操作成功" + row);

        statement.close();
        connection.close();
    }
}